# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=wasm-pack
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=0.13.1
pkgrel=1
pkgdesc="Your favorite rust -> wasm workflow tool! (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/rustwasm/wasm-pack"
msys2_references=(
  'archlinux: wasm-pack'
  'purl: pkg:cargo/wasm-pack'
)
license=('spdx:MIT OR Apache-2.0')
depends=(
  "${MINGW_PACKAGE_PREFIX}-rust-wasm"
  "${MINGW_PACKAGE_PREFIX}-bzip2"
  "${MINGW_PACKAGE_PREFIX}-zstd"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-mdbook"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
  "${MINGW_PACKAGE_PREFIX}-cmake"
)
source=("${url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "zstd-sys.tar.gz::https://crates.io/api/v1/crates/zstd-sys/2.0.11+zstd.1.5.6/download"
        "bzip2-sys.tar.gz::https://crates.io/api/v1/crates/bzip2-sys/0.1.11+1.0.8/download"
        "zstd-sys-remove-statik.patch"
        "bzip2-use-pkgconfig.patch")
sha256sums=('3c28be53174fd12a6f3c3a018f14c8383b2eec6c6699c74751c1f3c51a2346c0'
            '75652c55c0b6f3e6f12eb786fe1bc960396bf05a1eb3bf1f3691c3610ac2e6d4'
            '736a955f3fa7875102d57c82b8cac37ec45224a07fd32d58f9f7a186b6cd4cdc'
            '48f4900ceb02d3aaf9a1020f33d56629156e96759f456c0e7ca18bfcf910767b'
            'f73309fb98b89b2085bccee6ffb66820082628d096bc989922b25d33a1f5f378')

prepare() {
  cd "${_realname}-${pkgver}"

  patch -d ../bzip2-sys-0.1.11+1.0.8 -i ../bzip2-use-pkgconfig.patch
  patch -d ../zstd-sys-2.0.11+zstd.1.5.6 -i ../zstd-sys-remove-statik.patch
  cat >> Cargo.toml <<END

[patch.crates-io]
bzip2-sys = { path = "../bzip2-sys-0.1.11+1.0.8" }
zstd-sys = { path = "../zstd-sys-2.0.11+zstd.1.5.6" }
END

  cargo update -p bzip2-sys -p zstd-sys
  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd "${_realname}-${pkgver}"

  export ZSTD_SYS_USE_PKG_CONFIG=1
  cargo build --release --frozen

  cd docs
  mdbook build
}

check() {
  cd "${_realname}-${pkgver}"

  export ZSTD_SYS_USE_PKG_CONFIG=1
  cargo test --release --frozen
}

package_wasm-pack() {
  cd "${_realname}-${pkgver}"

  install -Dm755 "target/release/wasm-pack.exe" "${pkgdir}${MINGW_PREFIX}/bin/wasm-pack.exe"
  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/" LICENSE-{APACHE,MIT}
}

package_wasm-pack-docs() {
  pkgdesc+=" (Documentation)"
  depends=()

  cd "${_realname}-${pkgver}"

  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/html/"
  cp -r -t "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/html/" docs/book/*
}

# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
